Imports System.Data.SqlClient

'master
Public Class ChietTinh
    Private sMaLoaiChietTinh As String
    Private sSoPhieu As String
    Private dNgayLap As Date
    Private sKhachHang As String
    Private sDiaChi As String
    Private nVanChuyen As Decimal

    Private sSoHd As String
    Private sSeri As String
    Private dNgayHd As Date
    Private sMst As String

    Private sSoNt As String
    Private dNgayNt As Date
    Private bDaNt As Boolean

    Private sMa_DViDChinh As String
    Private sSo_Don As String

    Public Property MaLoaiChietTinh() As String
        Get
            Return sMaLoaiChietTinh
        End Get
        Set(ByVal Value As String)
            sMaLoaiChietTinh = Value
        End Set
    End Property

    Public Property SoPhieu() As String
        Get
            Return sSoPhieu
        End Get
        Set(ByVal Value As String)
            sSoPhieu = Value
        End Set
    End Property

    Public Property NgayLap() As Date
        Get
            Return dNgayLap
        End Get
        Set(ByVal Value As Date)
            dNgayLap = Value
        End Set
    End Property

    Public Property KhachHang() As String
        Get
            Return sKhachHang
        End Get
        Set(ByVal Value As String)
            sKhachHang = Value
        End Set
    End Property

    Public Property DiaChi() As String
        Get
            Return sDiaChi
        End Get
        Set(ByVal Value As String)
            sDiaChi = Value
        End Set
    End Property

    Public Property VanChuyen() As Decimal
        Get
            Return nVanChuyen
        End Get
        Set(ByVal Value As Decimal)
            nVanChuyen = Value
        End Set
    End Property

    Public Property SoHd() As String
        Get
            Return sSoHd
        End Get
        Set(ByVal Value As String)
            sSoHd = Value
        End Set
    End Property

    Public Property Seri() As String
        Get
            Return sSeri
        End Get
        Set(ByVal Value As String)
            sSeri = Value
        End Set
    End Property

    Public Property NgayHd() As Date
        Get
            Return dNgayHd
        End Get
        Set(ByVal Value As Date)
            dNgayHd = Value
        End Set
    End Property

    Public Property Mst() As String
        Get
            Return sMst
        End Get
        Set(ByVal Value As String)
            sMst = Value
        End Set
    End Property

    Public Property SoNt() As String
        Get
            Return sSoNt
        End Get
        Set(ByVal Value As String)
            sSoNt = Value
        End Set
    End Property

    Public Property NgayNt() As Date
        Get
            Return dNgayNt
        End Get
        Set(ByVal Value As Date)
            dNgayNt = Value
        End Set
    End Property

    Public Property DaNt() As Boolean
        Get
            Return bDaNt
        End Get
        Set(ByVal Value As Boolean)
            bDaNt = Value
        End Set
    End Property

    Public Property Ma_DViDChinh() As String
        Get
            Return sMa_DViDChinh
        End Get
        Set(ByVal Value As String)
            sMa_DViDChinh = Value
        End Set
    End Property

    Public Property So_Don() As String
        Get
            Return sSo_Don
        End Get
        Set(ByVal value As String)
            sSo_Don = value
        End Set
    End Property

End Class

Public Class ChietTinhF

    Public Function GetInThang() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetInThang"

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Char))
            .Parameters("@nam").Value = Project_Nam

            .Parameters.Add(New SqlClient.SqlParameter("@thang", SqlDbType.Char))
            .Parameters("@thang").Value = Project_Thang

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetLastSoPhieu() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetLastSoPhieu"

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Int))
            .Parameters("@nam").Value = Project_Nam

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetLastHoaDon() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetLastHoaDon"

            .Parameters.Add(New SqlClient.SqlParameter("@nam", SqlDbType.Decimal))
            .Parameters("@nam").Value = CDec(Project_Nam)

            .Parameters.Add(New SqlClient.SqlParameter("@thang", SqlDbType.Decimal))
            .Parameters("@thang").Value = CDec(Project_Thang)

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetWhereSoPhieu(ByVal SoPhieu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetWhereSoPhieu"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function ExistsSoPhieu(ByVal SoPhieu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_ExistsSoPhieu"

            .Parameters.Add(New SqlClient.SqlParameter("@SoPhieu", SqlDbType.Char))
            .Parameters("@SoPhieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Save(ByVal ChietTinh As ChietTinh) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_Save"

            .Parameters.Add(New SqlClient.SqlParameter("@maloaichiettinh", SqlDbType.Char))
            .Parameters("@maloaichiettinh").Value = ChietTinh.MaLoaiChietTinh

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = ChietTinh.SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@ngaylap", SqlDbType.SmallDateTime))
            .Parameters("@ngaylap").Value = ChietTinh.NgayLap

            .Parameters.Add(New SqlClient.SqlParameter("@khachhang", SqlDbType.NVarChar))
            .Parameters("@khachhang").Value = ChietTinh.KhachHang

            .Parameters.Add(New SqlClient.SqlParameter("@diachi", SqlDbType.NVarChar))
            .Parameters("@diachi").Value = ChietTinh.DiaChi

            .Parameters.Add(New SqlClient.SqlParameter("@vanchuyen", SqlDbType.Decimal))
            .Parameters("@vanchuyen").Value = ChietTinh.VanChuyen

            .Parameters.Add(New SqlClient.SqlParameter("@ma_dvidchinh", SqlDbType.VarChar))
            .Parameters("@ma_dvidchinh").Value = ChietTinh.Ma_DViDChinh

            .Parameters.Add(New SqlClient.SqlParameter("@so_don", SqlDbType.VarChar))
            .Parameters("@so_don").Value = ChietTinh.So_Don

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function UpdateNhiemThu(ByVal Dk As String, ByVal SoPhieu As String, ByVal Ngaynt As Date) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_UpdateNghiemThu"

            .Parameters.Add(New SqlClient.SqlParameter("@dk", SqlDbType.Char))
            .Parameters("@dk").Value = Dk

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@ngaynt", SqlDbType.SmallDateTime))
            .Parameters("@ngaynt").Value = Ngaynt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function UpdateHoaDon(ByVal ChietTinh As ChietTinh) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_UpdateHoaDon"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = ChietTinh.SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@sohd", SqlDbType.NVarChar))
            .Parameters("@sohd").Value = ChietTinh.SoHd

            .Parameters.Add(New SqlClient.SqlParameter("@seri", SqlDbType.NVarChar))
            .Parameters("@seri").Value = ChietTinh.Seri

            .Parameters.Add(New SqlClient.SqlParameter("@ngayhd", SqlDbType.SmallDateTime))
            .Parameters("@ngayhd").Value = ChietTinh.NgayHd

            .Parameters.Add(New SqlClient.SqlParameter("@mst", SqlDbType.NVarChar))
            .Parameters("@mst").Value = ChietTinh.Mst

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal SoPhieu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_Delete"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function ThanhToan(ByVal SoPhieu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_ThanhToan"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function GetListLoaiChietTinh(ByVal tungay As Date, ByVal denngay As Date) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetListLoaiChietTinh"

            .Parameters.Add(New SqlClient.SqlParameter("@tungay", SqlDbType.SmallDateTime))
            .Parameters("@tungay").Value = tungay

            .Parameters.Add(New SqlClient.SqlParameter("@denngay", SqlDbType.SmallDateTime))
            .Parameters("@denngay").Value = denngay

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetWhere(ByVal maloaichiettinh As String, ByVal tungay As Date, ByVal denngay As Date) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_GetWhere"

            .Parameters.Add(New SqlClient.SqlParameter("@maloaichiettinh", SqlDbType.Char))
            .Parameters("@maloaichiettinh").Value = maloaichiettinh

            .Parameters.Add(New SqlClient.SqlParameter("@tungay", SqlDbType.SmallDateTime))
            .Parameters("@tungay").Value = tungay

            .Parameters.Add(New SqlClient.SqlParameter("@denngay", SqlDbType.SmallDateTime))
            .Parameters("@denngay").Value = denngay

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    'report
    Public Function Print(ByVal SoPhieu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_Print"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function InChietTinh_KhachHang(ByVal Sophieu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_InChietTinh_KhachHang"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = Sophieu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function InChietTinh_DienLuc(ByVal Sophieu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinh_InChietTinh_DienLuc"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = Sophieu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

End Class

'detail
Public Class ChietTinhCt
    Private sSoPhieu As String
    Private nMa As Integer
    Private nSldl As Decimal
    Private nSlkh As Decimal
    Private nDgvl As Decimal
    Private nDgnc As Decimal
    Private nDgmtc As Decimal
    Private nSlntdl As Decimal
    Private nSlntkh As Decimal

    Public Property SoPhieu() As String
        Get
            Return sSoPhieu
        End Get
        Set(ByVal Value As String)
            sSoPhieu = Value
        End Set
    End Property

    Public Property Ma() As Integer
        Get
            Return nMa
        End Get
        Set(ByVal Value As Integer)
            nMa = Value
        End Set
    End Property

    Public Property Sldl() As Decimal
        Get
            Return nSldl
        End Get
        Set(ByVal Value As Decimal)
            nSldl = Value
        End Set
    End Property

    Public Property Slkh() As Decimal
        Get
            Return nSlkh
        End Get
        Set(ByVal Value As Decimal)
            nSlkh = Value
        End Set
    End Property

    Public Property Dgvl() As Decimal
        Get
            Return nDgvl
        End Get
        Set(ByVal Value As Decimal)
            nDgvl = Value
        End Set
    End Property

    Public Property Dgnc() As Decimal
        Get
            Return nDgnc
        End Get
        Set(ByVal Value As Decimal)
            nDgnc = Value
        End Set
    End Property

    Public Property Dgmtc() As Decimal
        Get
            Return nDgmtc
        End Get
        Set(ByVal Value As Decimal)
            nDgmtc = Value
        End Set
    End Property

    Public Property Slntdl() As Decimal
        Get
            Return nSlntdl
        End Get
        Set(ByVal Value As Decimal)
            nSlntdl = Value
        End Set
    End Property

    Public Property Slntkh() As Decimal
        Get
            Return nSlntkh
        End Get
        Set(ByVal Value As Decimal)
            nSlntkh = Value
        End Set
    End Property

End Class

Public Class ChietTinhCtF

    Public Function GetNew(ByVal MaLoaiChietTinh As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_GetNew"

            .Parameters.Add(New SqlClient.SqlParameter("@maloaichiettinh", SqlDbType.Char))
            .Parameters("@maloaichiettinh").Value = MaLoaiChietTinh

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function GetWhereSophieu(ByVal Sophieu As String) As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_GetWhereSophieu"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = Sophieu

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function Insert(ByVal ChietTinhCt As ChietTinhCt) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_Insert"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = ChietTinhCt.SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Int))
            .Parameters("@ma").Value = ChietTinhCt.Ma

            .Parameters.Add(New SqlClient.SqlParameter("@sldl", SqlDbType.Decimal))
            .Parameters("@sldl").Value = ChietTinhCt.Sldl

            .Parameters.Add(New SqlClient.SqlParameter("@slkh", SqlDbType.Decimal))
            .Parameters("@slkh").Value = ChietTinhCt.Slkh

            .Parameters.Add(New SqlClient.SqlParameter("@dgvl", SqlDbType.Decimal))
            .Parameters("@dgvl").Value = ChietTinhCt.Dgvl

            .Parameters.Add(New SqlClient.SqlParameter("@dgnc", SqlDbType.Decimal))
            .Parameters("@dgnc").Value = ChietTinhCt.Dgnc

            .Parameters.Add(New SqlClient.SqlParameter("@dgmtc", SqlDbType.Decimal))
            .Parameters("@dgmtc").Value = ChietTinhCt.Dgmtc

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal SoPhieu As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_Delete"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function DeleteRow(ByVal SoPhieu As String, ByVal Ma As Integer) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_DeleteRow"

            .Parameters.Add(New SqlClient.SqlParameter("@sophieu", SqlDbType.Char))
            .Parameters("@sophieu").Value = SoPhieu

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Int))
            .Parameters("@ma").Value = Ma

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function ExistsMa(ByVal Ma As Decimal) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ChietTinhCt_ExistsMa"

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Char))
            .Parameters("@ma").Value = Ma

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function


End Class

